package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.common.exception.CustException;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService {
    @Override
    public PageResponseResult findByNameAndPage(ChannelDTO dto) {
        // 1. 校验参数
        dto.checkParam(); // 检查分页
        // 2. 封装条件 执行查询
        Page<AdChannel> pageReq = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.lambdaQuery();
        // 频道名称
        if (StringUtils.isNotBlank(dto.getName())) {
            queryWrapper.like(AdChannel::getName, dto.getName());
        }
        // 状态
        if (dto.getStatus() != null) {
            queryWrapper.eq(AdChannel::getStatus, dto.getStatus());
        }
        // 序号升序
        queryWrapper.orderByAsc(AdChannel::getOrd);
        // 分页
        // 执行查询
        IPage<AdChannel> pageResult = this.page(pageReq, queryWrapper);
        // 3. 封装响应结果
        return new PageResponseResult(dto.getPage(), dto.getSize(),
                pageResult.getTotal(),
                pageResult.getRecords());
    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //1.名字不能为空
        String name = adChannel.getName();
        if (StringUtils.isBlank(name)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称不能为空！");
        }
        //2.名称<=10
        if (name.length() > 10) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称不能为空！");
        }
        //3.名称不可重复
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, name));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道名称已经存在！");
        }
        //4.保存到数据库
        adChannel.setCreatedTime(new Date());
        this.save(adChannel);
        //5.返回响应结果
        return ResponseResult.okResult();

    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        //1.参数校验
        if (adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道id不能为空！");

        }
        AdChannel oldChannel = getById(adChannel.getId());
        if (oldChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "频道不存在！");
        }
        //判断name是否为空
        String name = adChannel.getName();
        if (StringUtils.isNotBlank(name) && !name.equals(oldChannel.getName())) {
            if (name.length() > 10) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称不能为空！");
            }
            //3.名称不可重复
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, name));
            if (count > 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道名称已经存在！");
            }
        }
        //2.修改频道
        updateById(adChannel);
        //3.响应结果
        return ResponseResult.okResult();
    }

    @Transactional
    @Override
    public ResponseResult deleteById(Integer id) {
        AdChannel channel = getById(id);
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "频道id不存在！");
        }
        //判断频道状态是否启用
        if (channel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW, "频道状态未启用！");
        }
        removeById(id);
        if (id==48) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW);
        }
        return ResponseResult.okResult();
    }
}
